package com.M10.BE.inventory;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

import com.M10.BE.R;
import com.M10.BE.adapter.NullStringToEmptyAdapterFactory;
import com.M10.BE.entity.InventoryDetail;
import com.M10.BE.entity.NAsset;
import com.M10.BE.fragment.AssetListFragment;
import com.M10.BE.fragment.AssetLostListFragment;
import com.fn.useries.UHFApplication;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/14.
 */

public class CheckedInveDetailActivity extends FragmentActivity {
    public static final String TAG = "UncheckedInveDetailActivity";
    @BindView(R.id.mytabhost)
    FragmentTabHost tabHost;
    @BindView(R.id.tv_inve_back)
    TextView tv_back;
    @BindView(R.id.tv_inve_orderid)
    TextView tv_inve_orderid;
    @BindView(R.id.tv_inve_dept)
    TextView tv_inve_dept;
    @BindView(R.id.tv_inve_producttype)
    TextView tv_inve_producttype;
    @BindView(R.id.tv_inve_rfidtype)
    TextView tv_inve_labeltype;
    @BindView(R.id.tv_inve_setime)
    TextView tv_inve_setime;
    @BindView(R.id.tv_inve_jine)
    TextView tv_inve_jine;
    @BindView(R.id.tv_inve_total)
    TextView tv_inve_total;
    @BindView(R.id.tv_inve_checkednum)
    TextView tv_inve_checkednum;
    @BindView(R.id.tv_reinve)
    TextView tv_reinve;
    private LayoutInflater inflater;
    private String myTabText[];
    private HashMap<String, Fragment> fragments = new HashMap<>();
    private Class fragmentArray[] = {
            AssetListFragment.class, AssetLostListFragment.class
    };
    private Context context;
    String inventoryID;
    Gson gson;
    InventoryDetail inventoryDetail = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inve_detail_checked);
        ButterKnife.bind(this);
        context = CheckedInveDetailActivity.this;
        inventoryID = getIntent().getStringExtra("inventoryId");
        gson = new GsonBuilder().serializeNulls().
                registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
        initView();
        getDetail(inventoryID);
    }

    private void initView() {
        inflater = LayoutInflater.from(this);
        tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        for (int i = 0; i < 2; i++) {
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(i + "").setIndicator(getTabView(i));
            tabHost.addTab(tabSpec, fragmentArray[i], null);
        }
        tabHost.getTabWidget().setBackgroundColor(Color.argb(80, 255, 255, 2555));
        tabHost.setCurrentTab(0);

    }


    @OnClick(R.id.tv_inve_back)
    public void onViewClicked() {
        finish();
    }

    private View getTabView(int index) {
        myTabText = new String[]{
                "资产列表", "盘亏列表"
        };
        View view;
        view = LayoutInflater.from(CheckedInveDetailActivity.this).inflate(R.layout.fragment_relation_tab_view, null);
        TextView tabTextView = (TextView) view.findViewById(R.id.relation_tab);
        tabTextView.setText(myTabText[index]);
        return view;
    }

    private void setViewValue(InventoryDetail inventoryDetail) {
        tv_inve_orderid.setText(inventoryDetail.getInventoryID());
        tv_inve_dept.setText(inventoryDetail.getQueryOrgName());
        tv_inve_producttype.setText(inventoryDetail.getCzTypeName());
        tv_inve_labeltype.setText(inventoryDetail.getLabelTypeName());
        tv_inve_setime.setText(inventoryDetail.getQuerySTime() + "-" + inventoryDetail.getQueryETime());
        tv_inve_jine.setText(inventoryDetail.getQuerySAmount() + "~" + inventoryDetail.getQueryEAmount());
        tv_inve_total.setText(inventoryDetail.getQueryCount());
        tv_inve_checkednum.setText(inventoryDetail.getJudgeCount());
    }

    public void getDetail(String inventoryID) {
        OkGo.<String>get(UHFApplication.BASE_HEAD + "judge/APPShowUI")
                .tag(this)
                .params("id", inventoryID)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject object = new JSONObject(response.body());
                            inventoryDetail = gson.fromJson(object.toString(), InventoryDetail.class);
                            Log.e(TAG, object.toString());
                            setViewValue(inventoryDetail);
                            //总资产表
                            List<NAsset> result1 = inventoryDetail.getZclist();
                            Message message1 = new Message();
                            message1.obj = result1;
                            if (AssetListFragment.mHandler != null) {
                                AssetListFragment.mHandler.sendMessage(message1);
                            }
                            //盘亏表
                            List<NAsset> result2 = inventoryDetail.getPklist();
                            Message message2 = new Message();
                            message2.obj = result2;
                            if (AssetLostListFragment.mHandler != null) {
                                AssetLostListFragment.mHandler.sendMessage(message2);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }

    public void a() {
    }
}
